/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.vue;

import client.controleur.ApplicationControler;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.JRadioButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.SwingConstants;

/**
 *
 * @author Florian
 */
public class VueCalculPret extends JFrame {

    final public static String BTN_CALCULER = "Calculer";
    final public static String BTN_ANNUITE = "Annuite";
    final public static String BTN_NBANNEES = "Nb Annees";
    final public static String MENU_QUITTER = "Quitter";
    final public static String MENU_ANGLAIS = "Anglais";
    final public static String MENU_FRANCAIS = "Francais";
    
    private JMenuBar menuPrincipal;
    private JMenu menuFichier;
    private JMenu menuLangages;
    private JMenuItem jmi_Quitter;
    private JMenuItem jmi_Francais;
    private JMenuItem jmi_Anglais;
    private JPanel pnl_Principal;
    private JRadioButton rdbtn_Annuite;
    private JPanel jp_SaisieValeurs;
    private JLabel lbl_montant;
    private JTextField jtf_montant;
    private JLabel lbl_tauxInterets;
    private JTextField jtf_tauxInterets;
    private JLabel lbl_valeur3;
    private JTextField jtf_valeur3;
    private JPanel jp_ChoixCalcul;
    private JRadioButton rdbtn_NbAnnees;
    private JPanel jp_Resultats;
    private JButton btnCalculer;
    private JLabel lbl_Resultat;
    
    private static ResourceBundle bundle; // le bundle pour récupérer les messages

    public VueCalculPret(Locale locale) {
        
        bundle = ResourceBundle.getBundle("international.international", locale);
        
        setResizable(true);
        setVisible(true);
        setTitle("Application Services Client/Pret");

        //Instanciation des menus et des items
        menuPrincipal = new JMenuBar();
        menuFichier = new JMenu(bundle.getString("file"));
        jmi_Quitter = new JMenuItem(bundle.getString("quit"));
        menuLangages = new JMenu(bundle.getString("languages"));
        jmi_Francais = new JMenuItem(bundle.getString("french"));
        jmi_Anglais = new JMenuItem(bundle.getString("english"));

        //Mise en place des menus
        menuFichier.add(jmi_Quitter);
        menuLangages.add(jmi_Francais);
        menuLangages.add(jmi_Anglais);
        menuPrincipal.add(menuFichier);
        menuPrincipal.add(menuLangages);

        jmi_Francais.setActionCommand(MENU_FRANCAIS);
        jmi_Anglais.setActionCommand(MENU_ANGLAIS);
        
        //Raccourcis clavier
        jmi_Quitter.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));

        setJMenuBar(menuPrincipal);

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 300, 0, 0};
        gridBagLayout.rowHeights = new int[]{0, 141, 69, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        getContentPane().setLayout(gridBagLayout);

        jp_ChoixCalcul = new JPanel();
        GridBagConstraints gbc_jp_ChoixCalcul = new GridBagConstraints();
        gbc_jp_ChoixCalcul.insets = new Insets(0, 0, 5, 5);
        gbc_jp_ChoixCalcul.fill = GridBagConstraints.BOTH;
        gbc_jp_ChoixCalcul.gridx = 1;
        gbc_jp_ChoixCalcul.gridy = 0;
        getContentPane().add(jp_ChoixCalcul, gbc_jp_ChoixCalcul);
        jp_ChoixCalcul.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        rdbtn_Annuite = new JRadioButton(bundle.getString("annuity"));
        rdbtn_Annuite.setHorizontalAlignment(SwingConstants.CENTER);
        rdbtn_Annuite.setActionCommand(BTN_ANNUITE);
        jp_ChoixCalcul.add(rdbtn_Annuite);

        rdbtn_NbAnnees = new JRadioButton(bundle.getString("nb_years_bt"));
        rdbtn_NbAnnees.setHorizontalAlignment(SwingConstants.CENTER);
        rdbtn_NbAnnees.setActionCommand(BTN_NBANNEES);
        jp_ChoixCalcul.add(rdbtn_NbAnnees);

        jp_SaisieValeurs = new JPanel();
        GridBagConstraints gbc_jp_SaisieValeurs = new GridBagConstraints();
        gbc_jp_SaisieValeurs.insets = new Insets(0, 0, 5, 5);
        gbc_jp_SaisieValeurs.fill = GridBagConstraints.HORIZONTAL;
        gbc_jp_SaisieValeurs.gridx = 1;
        gbc_jp_SaisieValeurs.gridy = 1;
        getContentPane().add(jp_SaisieValeurs, gbc_jp_SaisieValeurs);
        GridBagLayout gbl_jp_SaisieValeurs = new GridBagLayout();
        gbl_jp_SaisieValeurs.columnWidths = new int[]{97, 182, 294, 0};
        gbl_jp_SaisieValeurs.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
        gbl_jp_SaisieValeurs.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_jp_SaisieValeurs.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        jp_SaisieValeurs.setLayout(gbl_jp_SaisieValeurs);

        lbl_montant = new JLabel(bundle.getString("amount"));
        GridBagConstraints gbc_lbl_valeur1 = new GridBagConstraints();
        gbc_lbl_valeur1.fill = GridBagConstraints.HORIZONTAL;
        gbc_lbl_valeur1.insets = new Insets(0, 0, 5, 5);
        gbc_lbl_valeur1.gridx = 1;
        gbc_lbl_valeur1.gridy = 1;
        jp_SaisieValeurs.add(lbl_montant, gbc_lbl_valeur1);

        jtf_montant = new JTextField();
        GridBagConstraints gbc_jtf_valeur1 = new GridBagConstraints();
        gbc_jtf_valeur1.fill = GridBagConstraints.HORIZONTAL;
        gbc_jtf_valeur1.insets = new Insets(0, 0, 5, 0);
        gbc_jtf_valeur1.gridx = 2;
        gbc_jtf_valeur1.gridy = 1;
        jp_SaisieValeurs.add(jtf_montant, gbc_jtf_valeur1);
        jtf_montant.setColumns(10);

        lbl_tauxInterets = new JLabel(bundle.getString("interests_rate"));
        GridBagConstraints gbc_lbl_valeur2 = new GridBagConstraints();
        gbc_lbl_valeur2.fill = GridBagConstraints.HORIZONTAL;
        gbc_lbl_valeur2.insets = new Insets(0, 0, 5, 5);
        gbc_lbl_valeur2.gridx = 1;
        gbc_lbl_valeur2.gridy = 2;
        jp_SaisieValeurs.add(lbl_tauxInterets, gbc_lbl_valeur2);

        jtf_tauxInterets = new JTextField();
        GridBagConstraints gbc_jtf_valeur2 = new GridBagConstraints();
        gbc_jtf_valeur2.fill = GridBagConstraints.HORIZONTAL;
        gbc_jtf_valeur2.insets = new Insets(0, 0, 5, 0);
        gbc_jtf_valeur2.gridx = 2;
        gbc_jtf_valeur2.gridy = 2;
        jp_SaisieValeurs.add(jtf_tauxInterets, gbc_jtf_valeur2);
        jtf_tauxInterets.setColumns(10);

        lbl_valeur3 = new JLabel(bundle.getString("number_years"));
        GridBagConstraints gbc_lbl_valeur3 = new GridBagConstraints();
        gbc_lbl_valeur3.fill = GridBagConstraints.HORIZONTAL;
        gbc_lbl_valeur3.insets = new Insets(0, 0, 5, 5);
        gbc_lbl_valeur3.gridx = 1;
        gbc_lbl_valeur3.gridy = 3;
        jp_SaisieValeurs.add(lbl_valeur3, gbc_lbl_valeur3);

        jtf_valeur3 = new JTextField();
        GridBagConstraints gbc_jtf_valeur3 = new GridBagConstraints();
        gbc_jtf_valeur3.fill = GridBagConstraints.HORIZONTAL;
        gbc_jtf_valeur3.insets = new Insets(0, 0, 5, 0);
        gbc_jtf_valeur3.gridx = 2;
        gbc_jtf_valeur3.gridy = 3;
        jp_SaisieValeurs.add(jtf_valeur3, gbc_jtf_valeur3);
        jtf_valeur3.setColumns(10);

        jp_Resultats = new JPanel();
        GridBagConstraints gbc_jp_Resultats = new GridBagConstraints();
        gbc_jp_Resultats.insets = new Insets(0, 0, 0, 5);
        gbc_jp_Resultats.fill = GridBagConstraints.BOTH;
        gbc_jp_Resultats.gridx = 1;
        gbc_jp_Resultats.gridy = 2;
        getContentPane().add(jp_Resultats, gbc_jp_Resultats);
        GridBagLayout gbl_jp_Resultats = new GridBagLayout();
        gbl_jp_Resultats.columnWidths = new int[]{220, 0};
        gbl_jp_Resultats.rowHeights = new int[]{29, 0, 0};
        gbl_jp_Resultats.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_jp_Resultats.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        jp_Resultats.setLayout(gbl_jp_Resultats);

        btnCalculer = new JButton(bundle.getString("calculate"));
        btnCalculer.setActionCommand(BTN_CALCULER);
        GridBagConstraints gbc_btnCalculer = new GridBagConstraints();
        gbc_btnCalculer.insets = new Insets(0, 0, 5, 0);
        gbc_btnCalculer.anchor = GridBagConstraints.NORTH;
        gbc_btnCalculer.gridx = 0;
        gbc_btnCalculer.gridy = 0;
        jp_Resultats.add(btnCalculer, gbc_btnCalculer);

        lbl_Resultat = new JLabel();
        GridBagConstraints gbc_lblResultat = new GridBagConstraints();
        gbc_lblResultat.gridx = 0;
        gbc_lblResultat.gridy = 1;
        jp_Resultats.add(lbl_Resultat, gbc_lblResultat);

        pnl_Principal = new JPanel();
        GridBagLayout gbl_pnl_Principal = new GridBagLayout();
        gbl_pnl_Principal.columnWidths = new int[]{30, 0, 26, 0};
        gbl_pnl_Principal.rowHeights = new int[]{60, 233, 50, 0};
        gbl_pnl_Principal.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
        gbl_pnl_Principal.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        pnl_Principal.setLayout(gbl_pnl_Principal);

        this.pack();
    }

    public void abonnementComponents(ApplicationControler ctrl) {
        btnCalculer.addActionListener(ctrl);
        rdbtn_Annuite.addActionListener(ctrl);
        rdbtn_NbAnnees.addActionListener(ctrl);
        jmi_Anglais.addActionListener(ctrl);
        jmi_Francais.addActionListener(ctrl);
        jmi_Quitter.addActionListener(ctrl);
    }
    
    public static ResourceBundle getBundle(){
        return bundle;
    }

    public void setLangueApplication(Locale locale) {
        //On vérifie si on a selectionné une langue différente de la langue
        //actuelle.
        if (!bundle.getLocale().equals(locale)) {
            //on récupère le bundle correspondant a la nouvelle locale
            bundle = ResourceBundle.getBundle("international.international", locale);
            
            menuFichier.setText(bundle.getString("file"));
            jmi_Quitter.setText(bundle.getString("quit"));
            menuLangages.setText(bundle.getString("languages"));
            jmi_Francais.setText(bundle.getString("french"));
            jmi_Anglais.setText(bundle.getString("english"));
            
            rdbtn_Annuite.setText(bundle.getString("annuity_bt"));
            rdbtn_NbAnnees.setText(bundle.getString("nb_years_bt"));
            
            btnCalculer.setText(bundle.getString("calculate"));
            
            lbl_montant.setText(bundle.getString("amount"));
            lbl_tauxInterets.setText(bundle.getString("interests_rate"));
            
            //Teste sur le choxi courant
            if(getTextChoix().equals(BTN_ANNUITE)){
                lbl_valeur3.setText(bundle.getString("number_years"));
            }else{
                lbl_valeur3.setText(bundle.getString("annuity"));
            }
            lbl_Resultat.setText("");
        }
    }

    /**
     * Retourne le texte saisi par l'utilisateur dans le champs montant 
     * @return 
     */
    public String getTextMontant() {
        return jtf_montant.getText();
    }

    public String getTextNbAnnuite() {
        return jtf_valeur3.getText();
    }

    public String getTextChoix() {
        if (rdbtn_Annuite.isSelected()) {
            return rdbtn_Annuite.getActionCommand();
        } else {
            return rdbtn_NbAnnees.getActionCommand();
        }
    }

    public String getTextAnnuite() {
        return jtf_valeur3.getText();
    }

    public String getTextTxInterets() {
        return jtf_tauxInterets.getText();
    }

    /**
     * Affiche le résultat de l'annuite dans le textField correspondant
     * @param reponse 
     */
    public void afficherResultatAnnuite(double reponse) {
        lbl_Resultat.setForeground(Color.BLACK);
        lbl_Resultat.setText(bundle.getString("annuity_result") + reponse);
    }

    /**
     * Affiche le résultat du nombre d'années dans le textField correspondant
     * @param reponse 
     */
    public void afficherResultatNbAnnees(double reponse) {
        lbl_Resultat.setForeground(Color.BLACK);
        lbl_Resultat.setText(bundle.getString("nb_years_result") + reponse + " " + bundle.getString("years"));
    }

    public void afficherMsgErreur(String msg) {
        //Affichage du message en rouge
        lbl_Resultat.setForeground(Color.RED);
        lbl_Resultat.setText(msg);
    }

    public void setChoixCalculAnnuite() {
        if(rdbtn_NbAnnees.isSelected()){
            rdbtn_NbAnnees.setSelected(false);
        }
        rdbtn_Annuite.setSelected(true);
        lbl_valeur3.setText(bundle.getString("number_years"));
    }

    public void setChoixCalculNbAnnees() {
        if(rdbtn_Annuite.isSelected()){
            rdbtn_Annuite.setSelected(false);
        }
        rdbtn_NbAnnees.setSelected(true);
        lbl_valeur3.setText(bundle.getString("annuity"));
    }
}

